package com.acwing.partition38;

import java.io.*;

/**
 * @author rkc
 * @date 2022/7/20 9:08
 */
public class AC3715最少交换次数_树状数组 {

    private static final int N = 1010;
    private static int[] temp = new int[N], c = new int[N];
    private static int n;

    private static StreamTokenizer tokenizer = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    private static PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        n = nextInt();
        int ans = 0;
        for (int i = 0; i < n; i++) {
            int x = nextInt();
            temp[x] += ask(n) - ask(x - 1);
            ans += temp[x];
            add(x, 1);
        }
        for (int i = 1; i <= n; i++) writer.print(temp[i] + " ");
        writer.format("\n%d", ans);
        writer.flush();
    }

    private static void add(int x, int k) {
        for (int i = x; i <= n; i += lowbit(i)) c[i] += k;
    }

    private static int ask(int x) {
        int res = 0;
        for (int i = x; i != 0; i -= lowbit(i)) res += c[i];
        return res;
    }

    private static int lowbit(int x) {
        return x & -x;
    }

    private static int nextInt() throws IOException {
        tokenizer.nextToken();
        return (int) tokenizer.nval;
    }
}
